package com.ruoyi.teacher.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.teacher.domain.TChapter;
import com.ruoyi.teacher.domain.TQuestion;
import com.ruoyi.teacher.mapper.TQuestionMapper;
import com.ruoyi.teacher.vo.TChapterVo;
import com.ruoyi.teacher.mapper.TChapterMapper;
import com.ruoyi.teacher.service.ITChapterService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 章节Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-03
 */
@Service
public class TChapterServiceImpl extends ServiceImpl<TChapterMapper, TChapter> implements ITChapterService {

    @Override
    public TableDataInfo<TChapterVo> queryList(TChapterVo entity) {
        return PageUtils.buildDataInfo(this.baseMapper.queryList(PageUtils.buildPage(), entity));
    }

    @Override
    public List<TChapterVo> queryAll(TChapterVo entity) {
        return this.baseMapper.queryList(entity);
    }

    @Override
    public TChapterVo queryById(Long id) {
        return this.baseMapper.queryById(id);
    }

    @Override
    public boolean saveChapters(List<TChapter> chapters) {
        // 获取题库id
        Long bankId = chapters.get(0).getbId();
//        Long bId = 1L;
        System.err.println(chapters);
        if (bankId == null) {
            throw new RuntimeException("没有题库id，不能添加章节");
        }

        // 给列表添加order_num，顺便检查题库id，添加到集合中
        int count = 0;
        for (TChapter cur : chapters) {
            cur.setOrderNum(++count);
        }

        // 删除当前题库的章节列表
        QueryWrapper<TChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("b_id", bankId);
        baseMapper.delete(queryWrapper);

        // 重新添加章节
        for (TChapter cur : chapters) {
            baseMapper.insert(cur);
        }

        return true;
    }

    @Autowired
    TQuestionMapper questionMapper;

    @Override
    public List<TChapterVo> getChapterVOS(Integer bId) {
        QueryWrapper<TChapter> cpqw = new QueryWrapper<>();
        cpqw.eq(bId != null, "b_id", bId);
        cpqw.orderByAsc("order_num");
        List<TChapter> list = baseMapper.selectList(cpqw);
        ArrayList<TChapterVo> voList = new ArrayList<>();
        for (TChapter chapter : list) {
            TChapterVo ChapterVo = new TChapterVo();
            BeanUtils.copyProperties(chapter, ChapterVo);
            QueryWrapper<TQuestion> qqw = new QueryWrapper<>();
            qqw.eq("cp_id", chapter.getId());
            Integer count = questionMapper.selectCount(qqw);
            ChapterVo.setQuestionCount(count);
            voList.add(ChapterVo);

        }
        System.out.println(list);
        System.out.println(voList);
        return voList;
    }
}
